package com.ymk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ymk.constant.MessageConstant;
import com.ymk.constant.RedisConstant;
import com.ymk.pojo.Setmeal;
import com.ymk.service.SetMealService;
import com.ymk.util.QiniuUtils;
import com.ymk.vo.PageResult;
import com.ymk.vo.QueryPageBean;
import com.ymk.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setMeal")
public class SetMealController {

    @Reference
    private SetMealService setMealService;

    @Autowired
    JedisPool jedisPool;

    //imgFile:需要跟页面el-upload里面的name保持一致
    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile){
        try {
            //获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            //找到.最后出现的位置
            int lastIndexOf = originalFilename.lastIndexOf(".");
            //获取文件后缀
            String suffix = originalFilename.substring(lastIndexOf);
            //使用UUID随机产生文件名称，防止同名文件覆盖
            String fileName = UUID.randomUUID().toString() + suffix;
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //图片上传成功
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);;
            //将上传图片名称存入Redis,基于Redis的Set集合存储
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    //新增
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")//权限校验
    public Result add(@RequestBody Setmeal setmeal,Integer[] travelGroupIds){
        try {
            setMealService.add(setmeal,travelGroupIds);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")//权限校验
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setMealService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }

    //条件查询
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")//权限校验
    public Result findById(Integer id){
        try {
            Setmeal setmeal = setMealService.findById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findTravelGroupItemBySetMealId")
    public List<Integer> findTravelGroupItemBySetMealId(Integer id){
        List<Integer> list = setMealService.findTravelGroupItemBySetMealId(id);
        return list;
    }

    //修改
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")//权限校验
    public Result edit(@RequestBody Setmeal setmeal,Integer[] travelItemIds){
        try {
            setMealService.edit(setmeal,travelItemIds);
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    //删除
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")//权限校验
    public Result delete(Integer id){
        try {
            setMealService.delete(id);
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }
}
